<template>
  <a-modal :open="open" width="960px" title="更换医护" :ok-button-props="{ disabled: !isCompleted }" centered @cancel="onCancel" @ok="onOk">
    <div class="w-full flex">
      <div class="w-1/3">
        <!-- <BaseTitle title="信息" /> -->
        <BedCard :data="data" class-name="flex-center py-12" active />
      </div>
      <div class="w-2/3 pl-6 border-l-2">
        <a-form :model="formState" :label-col="{ span: 9 }" class="mt-4 form-style-flex">
          <a-form-item label="护理组" class="w-1/2 mt-4">
            <SelectTable
              v-model:value="formState.nurseGroupIdName"
              class="w-full"
              :search-params="{ groupType: 2 }"
              type="护理医疗"
              @select="(item) => onSelectNursing('nurseGroupId', item)"
            />
          </a-form-item>
          <a-form-item label="医疗组" class="w-1/2 mt-4">
            <SelectTable
              v-model:value="formState.medicalGroupIdName"
              class="w-full"
              :search-params="{ groupType: 1 }"
              type="护理医疗"
              @select="(item) => onSelectNursing('medicalGroupId', item)"
            />
          </a-form-item>
          <a-form-item label="责任护士" class="w-1/2" v-bind="validateInfos.lastDutyNurseName">
            <SelectTable v-model:value="formState.lastDutyNurseName" class="w-full" type="护士" @select="(item) => onSelectDoctor('lastDutyNurse', item)" />
          </a-form-item>
          <a-form-item label="质控护士" class="w-1/2">
            <SelectTable v-model:value="formState.lastQcNurseName" class="w-full" type="护士" @select="(item) => onSelectDoctor('lastQcNurse', item)" />
          </a-form-item>
          <a-form-item label="护士长" class="w-1/2">
            <SelectTable v-model:value="formState.lastHeadNurseName" class="w-full" type="护士" @select="(item) => onSelectDoctor('lastHeadNurse', item)" />
          </a-form-item>
          <a-form-item label="责任医生" class="w-1/2" v-bind="validateInfos.lastDutyDoctorName">
            <SelectTable v-model:value="formState.lastDutyDoctorName" class="w-full" type="医生" @select="(item) => onSelectDoctor('lastDutyDoctor', item)" />
          </a-form-item>
          <a-form-item label="主治医生" class="w-1/2" v-bind="validateInfos.lastAttendingDoctorName">
            <SelectTable
              v-model:value="formState.lastAttendingDoctorName"
              class="w-full"
              type="医生"
              @select="(item) => onSelectDoctor('lastAttendingDoctor', item)"
            />
          </a-form-item>
          <a-form-item label="住院医生" class="w-1/2" v-bind="validateInfos.lastInpDoctorName">
            <SelectTable v-model:value="formState.lastInpDoctorName" class="w-full" type="医生" @select="(item) => onSelectDoctor('lastInpDoctor', item)" />
          </a-form-item>
          <a-form-item label="科主任" class="w-1/2">
            <SelectTable
              v-model:value="formState.lastDeptDirectorName"
              class="w-full"
              type="医生"
              @select="(item) => onSelectDoctor('lastDeptDirector', item)"
            />
          </a-form-item>
          <a-form-item label="主任(副)医生" class="w-1/2" v-bind="validateInfos.lastDirectorDoctorName">
            <SelectTable
              v-model:value="formState.lastDirectorDoctorName"
              class="w-full"
              type="医生"
              @select="(item) => onSelectDoctor('lastDirectorDoctor', item)"
            />
          </a-form-item>
          <!-- <a-form-item label="主管医生" class="w-1/2">
            <SelectTable v-model:value="formState.zgysmc" type="医生" @select="onSelectDocker($event, 'zgys')" />
          </a-form-item>
          <a-form-item label="床位医生" class="w-1/2">
            <SelectTable v-model:value="formState.cwysmc" type="医生" @select="onSelectDocker($event, 'cwys')" />
          </a-form-item> -->
          <!-- <a-form-item label="主治医生" class="w-1/2">
            <SelectTable v-model:value="formState.zzysmc" type="医生" @select="onSelectDocker($event, 'zzys')" />
          </a-form-item> -->
        </a-form>
      </div>
    </div>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SelectTable from '@/components/SelectTable'
import { post } from '@/utils/request'
import BedCard from '@/components/business/BedCard'

const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  data: {
    type: Object,
    default: () => ({})
  }
})

const formState = ref({
  mbbq: '',
  zcsj: dayjs().format('YYYY-MM-DD HH:mm:ss')
})
const rulesState = ref({
  lastDutyNurseName: [{ required: true, message: '请选择责任护士' }],
  lastDutyDoctorName: [{ required: true, message: '请选择责任医生' }],
  lastAttendingDoctorName: [{ required: true, message: '请选择主治医生' }],
  lastInpDoctorName: [{ required: true, message: '请选择住院医生' }],
  lastDirectorDoctorName: [{ required: true, message: '请选择主任(副)医生' }]
})
const emit = defineEmits(['update:open', 'close', 'success'])
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)
const onOk = async () => {
  await validate()
  const { code, msg } = await post('/inp/zyhs/doChangeCare', {
    inplsh: formState.value.inplsh,
    nurseGroupId: formState.value.nurseGroupId,
    medicalGroupId: formState.value.medicalGroupId,
    lastDutyNurse: formState.value.lastDutyNurse,
    lastQcNurse: formState.value.lastQcNurse,
    lastHeadNurse: formState.value.lastHeadNurse,
    lastDutyDoctor: formState.value.lastDutyDoctor,
    lastAttendingDoctor: formState.value.lastAttendingDoctor,
    lastDeptDirector: formState.value.lastDeptDirector,
    lastDirectorDoctor: formState.value.lastDirectorDoctor,
    lastInpDoctor: formState.value.lastInpDoctor
  })
  if (code !== 0) return message.error(msg || '操作失败')
  message.success(msg || '更换成功')
  onCancel()
  emit('success')
}
// 护理医疗
const onSelectNursing = (key, item) => {
  formState.value[key] = item.id
  formState.value[key + 'Name'] = item.groupName
}

// const onSelectDocker = (selected, key) => {
//   formState.value[key] = selected.empId
//   formState.value[key + 'mc'] = selected.empName
// }
const onSelectDoctor = (key, item) => {
  formState.value[key] = item.empId
  formState.value[key + 'Name'] = item.empName
}
watch(
  () => props.open,
  (val) => {
    if (val) {
      if (props.type === '转区') {
        const { transferDate, advMemo } = props.data.inpTransferZone
        formState.value = {
          ...props.data,
          zcsj: transferDate ? transferDate + '00:00:00' : '',
          cmemo: advMemo || ''
        }
      } else {
        formState.value = {
          ...props.data
        }
      }
    }
  }
)
const onCancel = () => {
  resetFields()
  emit('update:open', false)
  emit('close')
}
</script>
